package top.linyuxb.lycloud.config.starter.controller;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.linyuxb.lycloud.common.properties.LyConfigProperties;
import top.linyuxb.lycloud.config.starter.common.LyConfigService;

import java.util.Objects;

/**
 * @author linyuxb
 * @date 2024/1/13 4:20 下午
 * @desc
 */
@Slf4j
@RestController
@RequestMapping(value = "/lyConf")
public class LyConfController {
    @Value("${ly-config.user:}")
    private String user = StringUtils.EMPTY;
    @Value("${ly-config.user-password:}")
    private String userPassword = StringUtils.EMPTY;
    @Resource
    private LyConfigProperties lyRpcClientProperties;

    /**
     * 管理后台调整配置，配置主动刷新
     *
     * @param user
     * @param password
     * @return
     */
    @PostMapping(value = "/refreshConf")
    public String refreshConf(
            @RequestHeader(value = "user", defaultValue = "") String user,
            @RequestHeader(value = "password", defaultValue = "") String password) {
        log.info("lyConf refreshConf");
        if (!Objects.equals(this.user, user) || !Objects.equals(this.userPassword, password)) {
            return "接口请求非法！";
        }
        try {
            LyConfigService.updateValues(lyRpcClientProperties);
        } catch (Exception e) {
            log.error("LyConfigThread error", e);
        }
        return "SUCCESS";
    }

}
